﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;

namespace Dryricenet.houTai
{
    public partial class pay_Way : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Refresh();
                Literal1.Text = "以上数据共" + payBLL.paydataNumBll() + "条";
            }
        }
        //刷新
        public void Refresh()
        {
            GridView1.DataSource = payBLL.selectPayBll();
            GridView1.DataBind();
        }
        //查找
        protected void Search_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            if (name != "")
            {
                GridView1.DataSource = payBLL.payCXBll(name);
                GridView1.DataBind();
                
            }
            else {
                Refresh();
            }
        }
        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;//设置开启编辑
            Refresh();//刷新
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取绑定的主键ID值

            //获取用户修改的单元格里的值（cells[索引]）
            Pay p = new Pay();
            p.PayID= int.Parse(id);
            p.PayName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
            if (payBLL.updatePayBLL(p))
            {
                Response.Write("<script>alert('修改成功！')</script>");
            }
            else
            {
                Response.Write("<script>alert('修改失败！')</script>");
            }
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Refresh();
        }
        //分页
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;//设置显示用户选择的页码
            Refresh();
        }
        //导出数据表格
        protected void Export_Click(object sender, EventArgs e)
        {
            DataTable dt = payBLL.selectPayBll();
            CreateExcel(dt, "application/ms-excel", "Excel" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ".xls");//调用函数
        }
        /// <summary>
        /// DataTable导出到Excel
        /// </summary>
        /// <param name="dt">DataTable类型的数据源</param>
        /// <param name="FileType">文件类型</param>
        /// <param name="FileName">文件名</param>
        public void CreateExcel(DataTable dt, string FileType, string FileName)
        {
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.Buffer = true;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
            Response.ContentType = FileType;
            string colHeaders = string.Empty;
            string ls_item = string.Empty;
            DataRow[] myRow = dt.Select();
            int i = 0;
            int cl = dt.Columns.Count;
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                ls_item += dt.Columns[j].ColumnName + "\t"; //栏位：自动跳到下一单元格
            }
            ls_item = ls_item.Substring(0, ls_item.Length - 1) + "\n";
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }
                }
                Response.Output.Write(ls_item);
                ls_item = string.Empty;
            }
            Response.Output.Flush();
            Response.End();
        }
    }
}